﻿
//关闭当前页 返回父页面
function closePage(isReload) {
    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
    parent.layer.close(index); //再执行关闭
    if (isReload) {
        parent.search();
    }
}

layui.use(['form', 'layer', 'laydate'], function () {
    var form = layui.form,
        layer = layui.layer,
        laydate = layui.laydate;

    laydate.render({
        elem: '#tbPublishDate',
        type: 'date',
        trigger: 'click'
    });

    $(function () {
        if (app.getRequest('GUID')) {
            initData();
        }
        setKindEditor();
    });

    //初始化表单
    function initData() {
        var fieldData = {
            GUID: app.getRequest('GUID'),
            PostType: "get",
            ActionType: "Notice"
        };
        app.loadData("Notice.ashx", fieldData, function (data) {
            $('#tbNoticeType').val(data.NoticeType);
            if (data.Status == 1) {
                $('#rdStatus1').prop('checked', true);
            } else {
                $('#rdStatus0').prop('checked', true);
            }
            if (data.IsTip == 1) {
                $('#tbIsTip').prop('checked', true);
            } else {
                $('#tbIsTip').prop('checked', false);
            }
            $('#tbPublishDate').val(app.getTDate2(data.PublishDate));
            editor.html(HtmlUtil.htmlDecode(data.NoticeDescription));

            form.render();
        }, function (err) {
            layer.msg('请求失败');
        });
    }

    //保存
    form.on('submit(save)', function (data) {
        editor.sync(); //将KindEditor的数据同步到textarea标签。
        var fieldData = data.field;
        fieldData["GUID"] = app.getRequest('GUID');
        fieldData["ActionType"] = "Notice";
        fieldData["NoticeDescription"] = HtmlUtil.htmlEncode($('#txtNoticeDescription').val());
        fieldData["IsTip"] = 0;
        if ($("#tbIsTip").is(":checked")) {
            fieldData["IsTip"] = 1;
        }

        app.set("Notice.ashx", fieldData, function (data) {
            layer.msg(data.msg);
            if (data.success) {
                setTimeout(function () {
                    closePage(true);
                }, 1000);
            }
        }, function (err) {
            layer.msg('请求失败');
        });
        return false;
    });
});

var editor;
//初始化KindEditor
function setKindEditor() {
    editor = KindEditor.create('#txtNoticeDescription', {
        cssPath: '/resources/kindeditor/plugins/code/prettify.css',
        uploadJson: '/resources/kindeditor/asp.net/upload_json.ashx',
        fileManagerJson: '/resources/kindeditor/asp.net/file_manager_json.ashx',
        allowFileManager: true
    });
}
